class CreateRoles < ActiveRecord::Migration
  def self.up
    create_table "roles" do |t|
      t.column :name, :string
    end
    
    # generate the join table
    create_table "roles_users", :id => false do |t|
      t.column "role_id",  :integer
      t.column "user_id",  :integer
    end
    
    create_table "regions_users", :id => false do |t|
      t.column "user_id", :integer
      t.column "region_id", :integer
    end
    
    add_index "roles_users", "role_id"
    add_index "roles_users", "user_id"
    
    add_index "regions_users", "user_id"
    add_index "regions_users", "region_id"

    Role.create :name => "superadmin"
    Role.create :name => "admin"
    
    execute "INSERT INTO roles_users values (1,1)"
    execute "INSERT INTO roles_users values (2,2)"
    execute "INSERT INTO roles_users values (2,3)"
    execute "INSERT INTO roles_users values (2,4)"
    
    execute "INSERT INTO regions_users values (2,2)"
    execute "INSERT INTO regions_users values (3,2)"
    execute "INSERT INTO regions_users values (4,1)"
  end

  def self.down
    drop_table "roles"
    drop_table "roles_users"
  end
end